package com.region.integral.business_user.consume.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.business_user.consume.dao.IBreach;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.business_user.xml.Regulation;
import com.region.integral.business_user.xml.dto.Breach;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.BusinessUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iBreach")
public class BreachImpl extends BaseHibernate implements IBreach {

	@Override
	public void breach(int businessUserid, IntegralData integralData)
			throws IntegralException {
		BUIEntity buiEntity = getBUIEntity(businessUserid);
		if (buiEntity != null) {
			int currIntegral = buiEntity.getCurrIntegral();
			Regulation regulation = XMLRegulationUtil.unmarshaller2();
			List<Breach> breachs = regulation.getBreach();
			for (Breach breach : breachs) {
				int ruleIntegral = breach.getIntegral();
				if(currIntegral >= Math.abs(ruleIntegral)){
					int totalIntegral = currIntegral + ruleIntegral;
					buiEntity.setCurrIntegral(totalIntegral);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(businessUserid, ruleIntegral, "breach"));
					integralData.setSuccee(true);
				}
				else{
					integralData.setSuccee(false);
				}
			}

		}

	}
}
